/**
 * Implementation of Closure function
 *
 * author: Marcin Wachulski
 *
 */
#include "KOV.h"
#include <set>
#include <algorithm>
#include <iterator>

void ClosureForEmptySet(const AttributeSet &all_attrs, OAPair &oa_pair, FormalContext &local_fc) {
    if (local_fc.data.size() > 0) {
        OAPair &fc_last_p = local_fc.data.back();
        if (fc_last_p.first->data.size() == oa_pair.first->data.size()) {
            local_fc.data.pop_back();
            delete fc_last_p.first;
            delete fc_last_p.second;
        }
    }
    Closure(all_attrs, oa_pair, local_fc);
}

void Closure(const AttributeSet &all_attrs, OAPair &oa_pair, FormalContext &local_fc) {
    AttributeSet *all_FC_attrs = new AttributeSet();
    for (std::vector<OAPair>::const_iterator i = local_fc.data.begin(); 
        i != local_fc.data.end(); i++)
        all_FC_attrs->data.insert(i->second->data.begin(), i->second->data.end());
    // if oa_pair sets are stored in dynamic memory - the following line should be executed
    delete oa_pair.second;
    oa_pair.second = new AttributeSet(all_attrs);
    for (std::set<Attribute>::const_iterator i = all_FC_attrs->data.begin(); 
        i != all_FC_attrs->data.end(); i++)
        oa_pair.second->data.erase(*i);
    delete all_FC_attrs;

}